package net.janeluo.rs.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.janeluo.rs.entity.RS_Menus;
import net.janeluo.rs.entity.RS_Orders;
import net.janeluo.rs.entity.RS_Shops;
import net.janeluo.rs.entity.RS_SymNotice;
import net.janeluo.rs.entity.RS_User;

import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.service.Service;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

/**
 * 用户首页业务类
 * 
 * @author DLK)JaneLuo
 * @version 1.0.0
 */
@IocBean(fields = { "dao" })
public class UserIndexService extends Service {
    private static final Log log = Logs.getLog(UserIndexService.class);

    public UserIndexService() {
        super();
    }

    public UserIndexService(Dao dao) {
        super(dao);
    }
    
    /**
     * 用户首页初始化
     * 
     * @return
     */
    public Map<RS_Shops, List<RS_Menus>> init(){
        log.debug("============= 用户首页初始化处理开始 =============");
        
        // 查询店铺列表
        Criteria cri = Cnd.cri();
        List<RS_Shops> list = dao().query(RS_Shops.class, cri);
        
        Map<RS_Shops, List<RS_Menus>> menuMap= new LinkedHashMap<RS_Shops, List<RS_Menus>>();
        for (RS_Shops rs_Shops : list) {
            cri = Cnd.cri();
            cri.where().andEquals("sid", rs_Shops.getSid());
            List<RS_Menus> menulist = dao().query(RS_Menus.class, cri);
            menuMap.put(rs_Shops, menulist);
        }
        
        log.debug("============= 用户首页初始化处理结束 =============");
        return menuMap;
    }
    
    /**
     * 查询系统通知
     * 
     * @return
     */
    public List<RS_SymNotice> searchNoticeLst(){
        Criteria cri = Cnd.cri();
        List<RS_SymNotice> list = dao().query(RS_SymNotice.class, cri);
        return list;
    }
    
    /**
     * 用户新增
     * 
     * @param entity 用户实体
     * @return 用户实体
     */
    public RS_User insertUser(RS_User entity){
        return dao().insert(entity);
    }
    
    /**
     * 保存用户订单
     * 
     * @param midarr
     * @param mnamearr
     * @param nums
     */
    public void saveUserOrder(RS_User user, Integer[] midarr, String[] mnamearr, String[] nums, String totalPrice){
        log.debug("================= 保存用户订单处理开始 ================");
        
        // 获取当前系统时间
        Date date = new Date(); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");//可以方便地修改日期格
        String dateStr = dateFormat.format(date);
        
        final List<RS_Orders> lst = new ArrayList<RS_Orders>();
        for (int i = 0; i < midarr.length; i++) {
            RS_Orders o = new RS_Orders();
            Integer mid = midarr[i];
            String mname = mnamearr[i];
            String quantity = nums[i];
            
            o.setMid(mid);
            o.setMname(mname);
            o.setQuantity(quantity);
            o.setOrderTime(dateStr);
            o.setUserid(user.getUserid());
            o.setUserName(user.getUserName());
            o.setDepartment(user.getDepartment());
            lst.add(o);
        }
        
        
        // 更新余额
        final RS_User rs_User = dao().fetch(RS_User.class, Cnd.where("userid", "=", user.getUserid()));
        String balance = StringUtils.isNotEmpty(rs_User.getBalance()) ? rs_User.getBalance() : "0";
        BigDecimal bd = new BigDecimal(balance);
        BigDecimal decimal = bd.subtract(new BigDecimal(totalPrice));
        rs_User.setBalance(decimal.toString());
        Trans.exec(new Atom(){
            public void run(){
                dao().insert(lst);
                dao().update(rs_User);
            }
        });
        log.debug("================= 保存用户订单处理结束 ================");
    }
}
